define(() => {
    class CartPage {
        constructor() {
            this.cart_list = JSON.parse(localStorage.getItem('cart')) || [];
            this.ul = document.querySelector('#pro_content');
            this.input = document.querySelector('#ul1 .input');
            this.allPrice = document.querySelector('.allPriceNum');
            this.allNum = document.querySelector('.allNum');
            this.selectAll = document.querySelector('.selectAll');
            this.init();
        }
        init() {
            this.render();
            this.checkAll();
        }
        render() {
            if (!this.cart_list.length) {
                let str = `
                <li class="none">
                <p>看啥呢！啥也没有啊！</p>
                <span><a href="">去</a> 搞点东西回来啊！</span>
                </li>
                `
                this.ul.innerHTML = str;
            } else {
                let str = '';
                this.cart_list.forEach((item) => {
                    str += `
                    <li>
                        <div class="input11">
                            <input type="checkbox" data-id="${item.goods_id}" class="select" ${item.is_select ? "checked" : ""}>
                        </div>
                        <div class="product">
                            <div class="img">
                                <img src="${item.img_small_logo}" alt="">
                            </div>
                            <div class="title">
                                ${item.title}
                            </div>
                        </div>
                        <div class="information"></div>
                        <div class="title_price">
                            <p class="price_one">￥${item.price}</p>
                            <p class="price_two">￥${item.current_price}</p>
                        </div>
                        <div class="pro_num">
                            <span class="sub" data-id="${item.goods_id}">-</span>
                            <input type="text" value="${item.cart_number}" data-id="${item.goods_id}" class="txt">
                            <span class="add" data-id="${item.goods_id}">+</span>
                        </div>
                        <div class="pro_money">
                            ￥${item.cart_number * item.current_price}
                        </div>
                        <div class="caozuo">
                            <span class="del" data-id="${item.goods_id}">删除</span>
                        </div>
                    </li>
                    `

                })
                this.ul.innerHTML = str;

            }
            this.countNum1();
            this.inputChange();
            localStorage.setItem('cart',JSON.stringify(this.cart_list));
            // console.log(this.cart_list);
        }
        // 全选
        checkAll() {
            // 全选
            this.input.onclick = (e) => {
                var e = e || window.event;
                var target = e.target || e.srcElement;
                if (target.className == 'selectAll') {
                    this.cart_list.forEach((item) => {
                        item.is_select = target.checked;
                    })
                    this.render();
                }
            }
            // ul里面得功能
            this.ul.addEventListener('click', (e) => {
                var e = e || window.event;
                var target = e.target || e.srcElement;
                if (target.className == 'select') {
                    let targetId = target.dataset.id;
                    let goods = this.cart_list.find((item) => {
                        return item.goods_id == targetId
                    })
                    goods.is_select = target.checked;
                    console.log(goods.is_select);
                    this.selectAll.checked = this.cart_list.every((item) => {
                        return item.is_select
                    })
                    console.log(this.selectAll.checked);
                    this.render();
                }

                // 删除单个li
                if (target.className == 'del') {
                    let targetId = target.dataset.id;
                    console.log(targetId);
                    let goods = this.cart_list.find((item) => {
                        return item.goods_id == targetId
                    })
                    console.log(goods);
                    if (goods.is_select) {
                        if (confirm('你确定要删除这件商品吗?')) {
                            this.cart_list = this.cart_list.filter((item) => {
                                return item.goods_id != targetId
                            })
                        }
                        this.render();
                    }
                }

                // 增加商品数量

                if (target.className == 'add') {
                    let targetId = target.dataset.id;
                    let goods = this.cart_list.find((item) => {
                        return item.goods_id == targetId
                    })
                    if (goods.cart_number < goods.goods_number) {
                        goods.cart_number++
                    }
                    this.render();
                }

                // 减少商品数量
                if (target.className == 'sub') {
                    let targetId = target.dataset.id;
                    let goods = this.cart_list.find((item) => {
                        return item.goods_id == targetId
                    })
                    if (goods.cart_number > 1) {
                        goods.cart_number--
                    }
                    this.render();

                }
            })

        }
        // 输入框改变数据
        inputChange() {
            this.txt = document.querySelectorAll('.txt');
            this.txt.forEach((item) => {
                item.onblur = (e) => {
                    var e = e || window.event;
                    var target = e.target || e.srcElement;
                    let targetId = target.dataset.id;
                    let goods = this.cart_list.find((res) => {
                        return res.goods_id == targetId;
                    })
                    if (item.value <= goods.goods_number && item.value >= 2) {
                        goods.cart_number = item.value;
                    } else if (item.value > goods.goods_number) {
                        alert('亲！买点其他东西吧，这件商品没有这么多啦！');
                        goods.cart_number = goods.goods_number;
                    } else {
                        goods.cart_number = 1;
                    }
                    this.render();

                }
            })
        }
        // 数量计算
        countNum1() {
            let totalNum = 0
            let money = 0
            this.cart_list.forEach(item=>{
                if(item.goods_id){
                    money+= item.current_price*1 * item.cart_number*1;
                    totalNum+= item.cart_number
                }
            })

            this.selectAll.checked = this.cart_list.every((item) => {
                return item.is_select
            })
            
            this.allNum.innerHTML = totalNum
            this.allPrice.innerHTML = money
        }
    }
    new CartPage
});